#include "Date.h"
#include <sstream>
#include <iostream>
using namespace std;

//Constructeur
Date::Date(int jour,int mois,int annee) throw (int) :
    j_(jour),m_(mois),a_(annee) {

    if(m_ >12 || j_ > nbjours(m_,a_) || m_<= 0 || j_<= 0 ) {
            cout << j_<<"/"<<m_<<"/"<<a_ << " Date impossible\n" << endl;
            throw (1);
    }
}

Date::Date() {
    j_=0;
    m_=0;
    a_=0;
}

//Constr par recopie
Date::Date(Date const& d):
    j_(d.j_),m_(d.m_),a_(d.a_) {}


bool Date::isBisextile(int a){
    return ( a%4 == 0 && a%100 != 0 ) || a%100 ==0;
}

//Nombre de jours
int Date::nbjours( int mois,int annee){
    if (mois ==2){
            return isBisextile(annee) ? 29:28;
    }
    else if ((mois - 1)%7 %2 ==0) {return 31;}
    else return 30;
}

//Getters
int Date::getDay()const{ return j_;}
int Date::getMonth()const{ return m_;}
int Date::getYear()const{ return a_;}

string Date::getDate() const {
    stringstream ss;//create a stringstream
    ss << j_ << "/"<< m_ << "/" <<a_;//add number to the stream
    return ss.str();//return a string with the contents of the stream
}

//Lendemain
Date Date::lendemain() const {
        int j,m,a;
        j = j_+1;
        m = m_;
        a = a_;

        if ( j> nbjours(m,a)){
                j=1;
                m++;
                if ( m>12 ){
                        m = 1;
                        a++;
                }
        }
        return Date(j,m,a);
}

int operator ==( const Date& d1, const Date& d2 ){
        if ( d1.getDay() == d2.getDay() && d1.getMonth() == d2.getMonth() && d1.getYear() == d2.getYear()){
                return 1;
        }
        else return 0;
}

ostream& operator <<(ostream& os,const Date& d){
        os << d.getDay() << "/" << d.getMonth() << "/" << d.getYear();
        return os;
}